package com.ailanyin.admin.service.impl;


import com.ailanyin.admin.service.SysDictDataService;
import com.ailanyin.admin.service.SysUserService;
import com.ailanyin.common.service.RedisService;
import com.ailanyin.mapper.SysDictDataMapper;
import com.ailanyin.model.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 17:03
 */
@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisService redisService;
    @Resource
    private SysDictDataMapper dictDataMapper;

    @Value("${redis.database}")
    private String database;

    private static final String KEY = ":sys:dict:";
    /**
     * 获取字典列表
     *
     * @param dictData dictData
     * @return List<SysDictData>
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode id
     * @return 详情
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDictData(SysDictData dict) {
        dict.setCreateBy(sysUserService.getCurrentUser().getUserName());
        dict.setCreateTime(new Date());
        int row = dictDataMapper.insertDictData(dict);
        if (row > 0) {
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(dict.getDictType());
            addToRedis(dict.getDictType(), dictDataList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData dict = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(dict.getDictType());
            addToRedis(dict.getDictType(), dictDataList);

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDictData(SysDictData dictData) {
        dictData.setUpdateBy(sysUserService.getCurrentUser().getUserName());
        dictData.setUpdateTime(new Date());
        int row = dictDataMapper.updateDictData(dictData);
        if (row > 0) {
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType(dictData.getDictType());
            addToRedis(dictData.getDictType(), dictDataList);
        }
    }

    private void addToRedis(String type, List<SysDictData> dictDataList) {
        redisService.set(database + KEY + type, dictDataList);
    }

}
